﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Holoville.HOTween;
using Holoville.HOTween.Core;
public class AtweenPostion : Atween
{
    //相对移动
    public Tweener rltTo(GameObject target, float duration, int vx, int vy, EaseType easeType = EaseType.EaseInQuad, float delay = 0, TweenDelegate.TweenCallbackWParms callback = null, object[] para = null)
    {
        target.GetComponent<UIRect>().Update();
        return this.to(target, duration, target.transform.localPosition.x + vx, target.transform.localPosition.y + vy, 0f, easeType, delay, false, callback, para);
    }
    public Tweener rltFrom(GameObject target, float duration, int vx, int vy, EaseType easeType = EaseType.EaseOutQuad, float delay = 0, TweenDelegate.TweenCallbackWParms callback = null, object[] para = null)
    {
        target.GetComponent<UIRect>().Update();
        return this.to(target, duration, target.transform.localPosition.x + vx, target.transform.localPosition.y + vy, 0f, easeType, delay, true, callback, para);
    }

    public Tweener to(GameObject target, float duration, float x, float y, float z, EaseType easeType = EaseType.Linear, float delay = 0, bool isFrom = false, TweenDelegate.TweenCallbackWParms callback = null, object[] para = null)
    { 
        //如果有锚点，则先清除
        UIRect rect = target.GetComponent<UIRect>();
        if (rect && rect.isAnchored)
        {
            rect.leftAnchor.target = rect.rightAnchor.target = rect.topAnchor.target = rect.bottomAnchor.target = null;
        } 

        //
        Tweener t;
        TweenParms p = new TweenParms();
        p.Prop("localPosition", new Vector3(x, y, z));
        p.Ease(easeType);

        if (delay > 0)
        {
            p.Delay(delay);
        }
        //
        if (callback != null)
        {
            p.OnComplete(callback, para);
        }
        if (callback != null)
        {
            p.OnComplete(callback, para);
        }
        //是否From模式
        if (isFrom)
        {
            t = HOTween.From(target.transform, duration, p);
        }
        else
        {
            t = HOTween.To(target.transform, duration, p);
        }

        return t;
    }
}
